<?php

namespace app\index\model;

use  think\Db;

class DeliveryfollowModel
 {
     //用户列表
     static public function getList($param, $titles = null, $function = null)
     {
        $where=[];
        if (!empty($param['sku'])) {
             $where['d.sku']=$param['sku'];
         }
         if (!empty($param['code'])) {
             $where['p.code']=$param['code'];
         }
        $fields="p.warehouse_id_out,p.warehouse_id_rec,p.shop_id_rec,p.company_id_out,d.sku,d.sku_name,"
                . " case p.type when 1 then o.allocate_order_sn when 2 then p.code end as allocate_order_sn,"
                . "p.code,d.quantity,CASE p.type WHEN 1 THEN concat(o.allocate_order_sn, '_', d.sku ) WHEN 2 THEN concat(p.code, '_', d.sku ) END AS code_sku,p.type";
        $plan=Db::table('distribution_plan_detail')->alias('d')->join('distribution_plan p' ,'p.id = d.distribution_plan_id','left')
                ->join('allocate_order o' ,'o.code = p.code','left')->field($fields)->where($where)->buildSql();
        //店仓入库数据
        $warehouse=Db::table('shop_warehouse_in_detail')->alias('cd')->join('shop_warehouse_in c' ,'cd.shop_warehouse_in_id = c.id','left')
                 ->field('quantity_in warequantity,c.create_time as waredate,concat(business_sn,"_",sku) sncode')
                 ->where('status','=','1')->where('type','in',['1','2'])->buildSql();
        $sql=Db::table($plan)->alias('plan')->join("$warehouse w",'w.sncode=plan.code_sku','left');
        return \app\common\service\ToolService::downOrReturn($sql, $param, $titles,$function);
     }
 }

